package hkr.se1.cyvasse;

import Game.Game;
import android.graphics.drawable.Drawable;
import android.view.DragEvent;
import android.view.View;
import android.view.View.OnDragListener;

public class MyDragListener implements OnDragListener {

	private Game game;
	private int[][] availableMoves;
	int row;
	int col;

	public MyDragListener(Game game) {
		this.game = game;
	}

	@Override
	public boolean onDrag(View v, DragEvent event) {

		// Save the ImageView of the moved piece.
		TileView view = (TileView) event.getLocalState();
		int id = view.getId();
		int row = id / 8;
		int col = id % 8;

		if (game.getSetup() == Game.RUN_GAME) {
				availableMoves = game.getMoves(row, col);
		} else if (game.getSetup() == Game.PIECE_SETUP) {
			availableMoves = game.setupMoves(row, col);
		}

		BoardView parent = (BoardView) view.getParent().getParent();
		switch (event.getAction()) {
		case DragEvent.ACTION_DRAG_STARTED:

			if (game.getSetup() != Game.TERRAIN_SETUP) {
				for (int i = 0; i < 8; i++) {
					for (int j = 0; j < 8; j++) {
						parent.setShaderImage(i, j, availableMoves[j][i]);
					}
				}
			}

			break;
		case DragEvent.ACTION_DRAG_ENTERED:

			// Do Nothing
			break;
		case DragEvent.ACTION_DRAG_EXITED:
			// Do Nothing
			break;
		case DragEvent.ACTION_DROP:
			switch (game.getSetup()) {
			case 0:
				if (game.viablePiece(row, col)) {
					// Calculate the row of the Tile that the Piece was dropped
					// in.
					int dropRow = v.getId() / 8;

					// Calculate the column of the Tile that the Piece was
					// dropped
					// in.
					int dropCol = v.getId() % 8;

					// Get the value representing the movement possibilities for
					// the
					// piece at the dropped square.
					int currentMove = availableMoves[dropRow][dropCol];

					// Get the TileView that the piece will be moved to.
					TileView owner = (TileView) v;

					switch (currentMove) {
					case (Game.MOVE):
						movePiece(view, row, col, dropRow, dropCol, owner);
						clearShades(parent);
						break;
					case (Game.ATTACK):
						game.doMove(row, col, dropRow, dropCol);
						owner.clearPieceDrawable();
						clearShades(parent);
						break;
					case (Game.MOVE_ATTACK):
						movePiece(view, row, col, dropRow, dropCol, owner);
						clearShades(parent);
						break;
					default:
						break;
					}

					break;
				}
				break;
			case 1:
				// Calculate the row of the Tile that the Piece was dropped in.
				int dropRow = v.getId() / 8;

				// Calculate the column of the Tile that the Piece was dropped
				// in.
				int dropCol = v.getId() % 8;
				if (game.checkTerrainDrop(dropRow, dropCol)) {

					TileView owner = (TileView) v;
					game.terrainSwap(row, col, dropRow, dropCol);
					Drawable d1 = view.getTileImage();
					Drawable d2 = owner.getTileImage();
					view.setTileDrawable(d2);
					owner.setTileDrawable(d1);
				}
				break;
			case 2:
				if (game.viablePiece(row, col)) {

					int endRow = v.getId() / 8;

					int endCol = v.getId() % 8;

					int currentMove = availableMoves[endRow][endCol];

					if (currentMove == 1) {
						TileView owner = (TileView) v;
						movePiece(view, row, col, endRow, endCol, owner);
						clearShades(parent);
					}

					break;
				}
				break;
			}

		case DragEvent.ACTION_DRAG_ENDED:

			break;
		default:
			break;
		}
		return true;
	}

	private void movePiece(TileView view, int row, int col, int dropRow,
			int dropCol, TileView owner) {
		// Set the image of the Piece in the new TileView
		owner.setPieceDrawable(view.getPieceImage());

		// Move the piece in the board array
		game.doMove(row, col, dropRow, dropCol);

		// Remove the image of the moved piece from parent.
		view.clearPieceDrawable();
	}

	private void clearShades(BoardView parent) {
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				parent.setShaderImage(i, j, 0);
			}
		}
	}

}
